package com.fingard.dsp.bank.directbank.nccb01;

import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class NCCB01SinglePay extends NCCB01Base implements ISinglePay {
    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();
        if ("190801".equals(p_req1908.reqHeader.commandCode)) {//同行
            tmpRetDetail = bankInnerTransfer(p_req1908);
        } else if("190802".equals(p_req1908.reqHeader.commandCode)) {//跨行
            tmpRetDetail = crossBankTransfer(p_req1908);
        } else{
            retAts.writeRespCode("0");
            retAts.writeRespInfo("只支持同行转账和跨行转账");
        }
        if ("-1".equals(tmpRetDetail.respCode) && StringHelper.isNullOrEmpty(tmpRetDetail.reqSeqID)) {
            tmpRetDetail.reqSeqID = p_req1908.singleDetail.askNumber;
        }
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    //跨行转账
    private RetAtsSglDetail crossBankTransfer(ReqSingle p_req1908) throws Exception {
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        NCCB01Header header = new NCCB01Header();
        header.transCode = "Transfer.do";
        header.msgType = "1";
        header.serialNo = p_req1908.singleDetail.askNumber;
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入通用的头部节点
        String tmpCur = convertCurToBank( p_req1908.singleDetail.payCur);
        writeCommonHeader(xmlWriter, header);
        xmlWriter.writeStartElement("Body");
        xmlWriter.writeElementString("PayerAcNo", p_req1908.singleDetail.payAct);
        String payerBankAcType = getBankConfig().getMapValueByKey("actCostItemCode",p_req1908.singleDetail.payAct);
        xmlWriter.writeElementString("PayerBankAcType", payerBankAcType == "" ? "01":payerBankAcType); //账户类型
        xmlWriter.writeElementString("PayerCurrency",tmpCur); //币种
        xmlWriter.writeElementString("Amount", p_req1908.singleDetail.payAmount);
        xmlWriter.writeElementString("PayeeAcNo",p_req1908.singleDetail.recAct);
        xmlWriter.writeElementString("PayeeAcName",p_req1908.singleDetail.recName);
        xmlWriter.writeElementString("UnionDeptId",p_req1908.singleDetail.cnapsCode);
        xmlWriter.writeElementString("Priority", "1");//加急
        xmlWriter.writeElementString("Remark", p_req1908.singleDetail.transInfo);
        xmlWriter.writeElementString("SysFlag","1");
        xmlWriter.writeEndDocument();
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(),header);
        if (tmpStrRet[0].length() == 0) {
            StringBuilder tmpSbMsg = new StringBuilder();
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
            if (tmpBkReader != null) {
                String tmpRetCode = tmpBkReader.getFirstText("/Message/Head/RejCode");
                String respondInfo = tmpBkReader.getFirstText("/Message/Head/RejMsg");
                if(tmpRetCode.equals("FIN0000")) {
                    retDetail.respCode = "0";
                    retDetail.reqSeqID = header.serialNo ;
                    String transState = tmpBkReader.getFirstText("/Message/Body/_JnlStatus");
                    if(transState.equals("0")){
                        retDetail.respInfo = "交易成功";
                    }else if(transState.equals("1")){
                        retDetail.respInfo = "通讯失败";
                    }else if(transState.equals("2")){
                        retDetail.respInfo = "交易失败";
                    }else if(transState.equals("3")){
                        retDetail.respInfo = "网银异常";
                    }else if(transState.equals("6")){
                        retDetail.respInfo = "业务已处理";
                    }else if(transState.equals("w")){
                        retDetail.respInfo = "待银行审核";
                    }else {
                        retDetail.respInfo = "银行未知定义返回";
                    }
                    retDetail.transState = p_req1908.reqHeader.commandCode + tmpBkReader.getFirstText("/Message/Body/_JnlStatus");
                    retDetail.payInfoCode = retDetail.transState;
                }else{
                    retDetail.respCode = "-1";
                    retDetail.respInfo = respondInfo;
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpSbMsg.toString();
            }
        } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
            retDetail.respCode = "0";
            retDetail.respInfo = tmpStrRet[1];
            retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
            retDetail.transState = PaymentState.FAIL_Payment;
            retDetail.payInfoCode = tmpStrRet[0];
        } else {
            retDetail.respCode = "-1";
            retDetail.respInfo = tmpStrRet[1];
        }
        return retDetail;
    }

    //同行转账
    private RetAtsSglDetail bankInnerTransfer(ReqSingle p_req1908) throws Exception {
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        NCCB01Header header = new NCCB01Header();
        header.transCode = "BankInnerTransfer.do";
        header.msgType = "1";
        header.serialNo = p_req1908.singleDetail.askNumber;
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入通用的头部节点
        String tmpCur = convertCurToBank( p_req1908.singleDetail.payCur);
        writeCommonHeader(xmlWriter, header);
        xmlWriter.writeStartElement("Body");
        xmlWriter.writeElementString("PayerAcNo", p_req1908.singleDetail.payAct);
        xmlWriter.writeElementString("PayerBankAcType","01"); //子账户
        xmlWriter.writeElementString("PayerCurrency",tmpCur); //币种
        xmlWriter.writeElementString("Amount", p_req1908.singleDetail.payAmount);
        xmlWriter.writeElementString("PayeeAcNo",p_req1908.singleDetail.recAct);
        xmlWriter.writeElementString("PayeeAcName",p_req1908.singleDetail.recName);
        xmlWriter.writeElementString("Remark", p_req1908.singleDetail.transInfo);
        xmlWriter.writeElementString("SysFlag","0");
        xmlWriter.writeEndDocument();
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(),header);
        if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/Message/Head/RejCode");
                    String respondInfo = tmpBkReader.getFirstText("/Message/Head/RejMsg");
                    if(tmpRetCode.equals("FIN0000")) {
                        retDetail.respCode = "0";
                        retDetail.reqSeqID = tmpBkReader.getFirstText("/Message/Body/_JnlNo");
                        retDetail.transState = p_req1908.reqHeader.commandCode + tmpBkReader.getFirstText("/Message/Body/_JnlStatus");
                        String transState = tmpBkReader.getFirstText("/Message/Body/_JnlStatus");
                        if(transState.equals("0")){
                            retDetail.respInfo = "交易成功";
                        }else if(transState.equals("W")){
                            retDetail.respInfo = "待银行审核";
                        }else{
                            retDetail.respInfo = "交易失败";
                        }
                        retDetail.payInfoCode = retDetail.transState;
                    }else{
                        retDetail.respCode = "-1";
                        retDetail.respInfo = respondInfo;
                    }
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpSbMsg.toString();
                }
        } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
            retDetail.respCode = "0";
            retDetail.respInfo = tmpStrRet[1];
            retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
            retDetail.transState = PaymentState.FAIL_Payment;
            retDetail.payInfoCode = tmpStrRet[0];
        } else {
            retDetail.respCode = "-1";
            retDetail.respInfo = tmpStrRet[1];
        }
        return retDetail;
    }

    //状态查询
    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        NCCB01Header header = new NCCB01Header();
        header.transCode = "etrsstatqry.TransStatQryRes.do";

        header.msgType = "0";
        header.serialNo = p_req1944.reqHeader.transSeqID;
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        writeCommonHeader(xmlWriter, header);
        xmlWriter.writeStartElement("Body");
        xmlWriter.writeElementString("OrigHttpOtherJnlNo",  p_req1944.reqHeader.reqSeqID);//第三方流水号
        xmlWriter.writeEndDocument();
        try {
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(),header);
            if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpRetCode = tmpBkReader.getFirstText("/Message/Head/RejCode");
                        String respondInfo = tmpBkReader.getFirstText("/Message/Head/RejMsg");
                        if(tmpRetCode.equals("FIN0000")) {
                            retDetail.reqSeqID = p_req1944.askNumber;
                            retDetail.respCode = "0";
                            retDetail.respInfo = tmpBkReader.getFirstText("/Message/Body/ErrMsg");
                            String transState = tmpBkReader.getFirstText("/Message/Body/TrsState");
                            if(transState.equals("0")){
                                retDetail.respInfo = "交易成功";
                            }else if(transState.equals("1")){
                                retDetail.respInfo = "通讯失败";
                            }else if(transState.equals("2")){
                                retDetail.respInfo = "交易失败";
                            }else if(transState.equals("3")){
                                retDetail.respInfo = "网银异常";
                            }else if(transState.equals("6")){
                                retDetail.respInfo = "业务已处理";
                            }else if(transState.equals("7")){
                                retDetail.respInfo = "约定成功";
                            }else if(transState.equals("8")){
                                retDetail.respInfo = "批量提交成功";
                            }else if(transState.equals("9")){
                                retDetail.respInfo = "未处理";
                            }else if(transState.equals("W")){
                                retDetail.respInfo = "待银行审核";
                            }else if(transState.equals("R")){
                                retDetail.respInfo = "银行拒绝";
                            }else if(transState.equals("S")){
                                retDetail.respInfo = "等待审核员审核";
                            } else {
                                retDetail.respInfo = "银行未知定义返回";
                            }
                            retDetail.transState = "190802" + tmpBkReader.getFirstText("/Message/Body/TrsState");
                            retDetail.payInfoCode = retDetail.transState;
                        }else{
                            retDetail.respCode = "-1";
                            retDetail.respInfo = respondInfo;
                        }
                    } else {
                        retDetail.respCode = "-1";
                        retDetail.respInfo = tmpSbMsg.toString();
                    }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            retDetail.respCode = "-1";
            retDetail.respInfo = e.getMessage();
            e.printStackTrace();
        }
        retAts.writeRetDetail(retDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
